package com.design.proxy.dynamicproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @auther LiZiBa
 * @date 2020/8/9 10:38
 * @description: cglib 和 jdk proxy 的应用区别是
 *                      cglib以继承实现类重写所有方法
 *                      jdk proxy需要类实现接口才能在底层调用接口的方法实现
 **/
public class JDKMeipo implements InvocationHandler {

    private IPerson target;

    public IPerson getInstance(IPerson target){
        this.target =target;
        Class<? extends IPerson> clazz = target.getClass();

        return (IPerson) Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(),this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        before();
        Object result = method.invoke(this.target, args);
        after();
        return null;
    }

    private void after() {
        System.out.println("交往");
    }

    private void before() {
        System.out.println("给钱");
    }
}
